-module(clique).
-export([cliques/2, cliques/5]).

cliques(G, IO)->
    cliques(graph:v(G),
	    [],
	    [],
	    G, IO).

cliques(N,D,S,G, IO)->
    case set:empty(set:join(N,D)) of
	true ->
	    io:fwrite(IO, "~w ~n",[S]);
	false -> case set:empty(N) of
		    false ->
			F = set:get_el(N),
			NewN = set:diff(N, [F]),
			cliques(set:cross(NewN, graph:adj(F, G)),
				 	set:cross(D,	graph:adj(F, G)),
				 	set:add(F,S),
				 	G,IO),
			loop(NewN,
			     set:add(F,D),
			     S,
			     set:cross(NewN,
						   set:diff(graph:v(G),graph:adj(F,G))),
			      G,IO);
		    true ->ok
		end
    end.

loop(_,_,_,[],_,_)->
    ok;
loop(N,D,S, [F | Tail] , G, IO)->
    NewN = set:diff(N, [F]),
    cliques(set:cross(NewN, graph:adj(F,G)),
	    	set:cross(D,    graph:adj(F,G)),
	    	set:add(F,S),
	    	G,IO),
    loop(NewN,
		 set:add(F,D),
		 S,
		 Tail,
		 G,
		 IO).
	    
    
    
